package com.group.nepm.statisticDataManagement.controller;

import com.group.common.utils.MyUtils;
import com.group.nepg.entity.*;
import com.group.nepm.statisticDataManagement.entity.ProvinceStatisticDataLoader;
import com.group.nepm.statisticDataManagement.entity.StatisticData;
import com.group.neps.entity.Province;
import com.group.neps.entity.RowAqiData;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;

import java.util.*;

public class ProvinceStatisticController {
    @FXML
    TableColumn provinceCode;
    @FXML
    TableColumn provinceName;
    @FXML
    TableColumn so2Count;
    @FXML
    TableColumn coCount;
    @FXML
    TableColumn pmCount;
    @FXML
    TableColumn aqiCount;
    @FXML
    TableView tableView;
    private ObservableList provinceStatisticDataList = FXCollections.observableArrayList();


    public void adminOut(ActionEvent event) {
        MyUtils.navigateToView(event, "group/nepm/common/view/NEPMLoginView.fxml");
    }
    public void toPublicSuperviseDataList(ActionEvent event) {
        MyUtils.navigateToView(event, "group/nepm/publicSuperviseDataManagement/view/PublicSuperviseDataList.fxml");
    }
    public void toConfirmAqiDataList(ActionEvent event) {
        MyUtils.navigateToView(event, "group/nepm/publicSuperviseDataManagement/view/ConfirmAqiDataList.fxml");
    }
    public void toProvinceStatistic(ActionEvent event) {
        MyUtils.navigateToView(event, "group/nepm/statisticDataManagement/view/provinceStatistic.fxml");
    }
    public void toAqiDistributionStatistic(ActionEvent event) {
        MyUtils.navigateToView(event, "group/nepm/statisticDataManagement/view/aqiDistributionStatistic.fxml");
    }
    public void toAqiTendencyStatistic(ActionEvent event) {
        MyUtils.navigateToView(event, "group/nepm/statisticDataManagement/view/aqiTendencyStatistic.fxml");
    }
    public void toOtherDataStatistic(ActionEvent event) {
        MyUtils.navigateToView(event, "group/nepm/statisticDataManagement/view/otherDataStatistic.fxml");
    }
    public void initialize() {
        String statisticDataPath = "group/data/statistic_data_arraylist.dat";
        String locationMapPath = "group/data/location_hashmap.dat";

        ArrayList<StatisticData> statisticDataList = MyUtils.readFileToArrayList(statisticDataPath);
        HashMap<String, Province> locationMap = MyUtils.readFileToHashMap(locationMapPath);

        if (statisticDataList == null || locationMap == null) {
            System.out.println("无法读取数据文件！");
            return;
        }

        Map<String, ProvinceStatisticDataLoader> statisticsMap = new HashMap<>();
        for (Province province : locationMap.values()) {
            statisticsMap.put(province.getCode(), new ProvinceStatisticDataLoader(
                    province.getCode(),
                    province.getName(),
                    0,
                    0,
                    0,
                    0
            ));
        }

        for (StatisticData data : statisticDataList) {
            RowAqiData rowAqiData = data.getRowAqiData();
            AccurateAqiData accurateAqiData = data.getAccurateAqiData();

            if (rowAqiData == null || rowAqiData.getLocation() == null || rowAqiData.getLocation().getProvince() == null) {
                continue;
            }

            Province province = rowAqiData.getLocation().getProvince();
            ProvinceStatisticDataLoader item = statisticsMap.get(province.getCode());

            if (item == null) {
                continue;
            }

            if (accurateAqiData.getSo2Level() != null && accurateAqiData.getSo2Level().ordinal() >= 4) {
                item.setSo2Count(item.getSo2Count() + 1);
            }

            if (accurateAqiData.getCoLevel() != null && accurateAqiData.getCoLevel().ordinal() >= 4) {
                item.setCoCount(item.getCoCount() + 1);
            }

            if (accurateAqiData.getPmLevel() != null && accurateAqiData.getPmLevel().ordinal() >= 4) {
                item.setPmCount(item.getPmCount() + 1);
            }

            if (accurateAqiData.getAccurateAqiLevel() != null && accurateAqiData.getAccurateAqiLevel().ordinal() >= 4) {
                item.setAqiCount(item.getAqiCount() + 1);
            }
        }

        List<ProvinceStatisticDataLoader> dataList = new ArrayList<>(statisticsMap.values());
        provinceStatisticDataList.addAll(dataList);

        Map<TableColumn<ProvinceStatisticDataLoader, ?>, String> columnMapping = new HashMap<>();
        columnMapping.put(provinceCode, "provinceCode");
        columnMapping.put(provinceName, "provinceName");
        columnMapping.put(so2Count, "so2Count");
        columnMapping.put(coCount, "coCount");
        columnMapping.put(pmCount, "pmCount");
        columnMapping.put(aqiCount, "aqiCount");
        MyUtils.showTableView(tableView, dataList, columnMapping);
    }

}
